package ch.areion.observator;

import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * A label / text field pair.
 * @author leupibr
 */
public class InfoItem {
	/**
	 * The id (and label text) of the item
	 */
	private String id;

	/**
	 * The label of the item.
	 */
	private JLabel label;
	/**
	 * The text field of the item.
	 */
	private JTextField textfield;

	/**
	 * Default constructor if no initial text should be set.
	 * @param id 
	 */
	public InfoItem(String id) {
		this(id, "");
	}

	/**
	 * Constructs a new information item.
	 * @param id The id of the item
	 * @param data The initial text to set
	 */
	public InfoItem(String id, String data) {
		label = new JLabel(id + ":");
		textfield = new JTextField("");
		textfield.setEditable(false);
		update(data);
	}

	/**
	 * Sets the text of the item.
	 * @param text The text of the item
	 */
	public void update(String text) {
		textfield.setText(text);
		textfield.setHorizontalAlignment(JTextField.LEFT);
		
		// maybe it can be set numerical?
		try {
			Double.parseDouble(text);
			textfield.setHorizontalAlignment(JTextField.RIGHT);
		}
		catch(Exception e){}
	}

	/**
	 * Return the label of the item.
	 * @return The label
	 */
	public JLabel getLabel() {
		return this.label;
	}

	/**
	 * Returns the text field of the item.
	 * @return The text field
	 */
	public JTextField getTextField() {
		return this.textfield;
	}
	
	/**
	 * Returns the id of the text item.
	 * @return
	 */
	public String getID() {
		return this.id;
	}

}
